package com.javaee.aop;



import com.javaee.cart.ShoppingCart;
import com.javaee.domain.Product;
import com.javaee.service.ProductService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class ProductAdvice {
    private ProductService productService;

    @Autowired
    public ProductAdvice(ProductService productServic) {
        this.productService = productService;
    }

    @Before("execution(* com.javaee.cart.ShoppingCart.add(..)) && args(product)")
    public void checkStock(JoinPoint joinPoint, Product product) {
        // 在添加商品到购物车之前，检查商品库存是否充足
        Product foundProduct = productService.findById(product.getId());
        if (foundProduct == null || foundProduct.getStock() < 1) {
            System.out.println("商品库存不足");
        }
    }

    @AfterReturning("execution(* com.javaee.cart.ShoppingCart.remove(..)) && args(productId)")
    public void logRemove(JoinPoint joinPoint, String productId) {
        // 记录删除操作的日志
        System.out.println("已删除商品 ID: " + productId);
    }

    @AfterReturning("execution(* com.javaee.cart.ShoppingCart.listAll())")
    public void logListAll(JoinPoint joinPoint) {
        // 记录获取操作的日志
        System.out.println("已获取购物车商品列表");
    }
}

